/**
 *  版权所有 Copyright@2016年1月29日
 */
package com.fairy.core.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
 * [DateUtil.java]
 *
 * @ProjectName: [dubbo_core]
 * @Author: [waysun]
 * @CreateDate: [2016年1月29日 上午10:57:46]
 * @Update: [说明本次修改内容] BY [waysun] [2016年1月29日 上午10:57:46]
 * @Version: [v1.0]
 */

public class DateUtil {
	   /**
     * <p>获得时间对应的星期,2016-01-29-->星期五</p>
     * @param dateString
     */
    public static String getWeek(String dateString)
    {
    	  String week="";
          final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };
          SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm");
          Calendar calendar = Calendar.getInstance();
          @SuppressWarnings("unused")
          Date date = new Date();
          try
          {
              date = sdfInput.parse(dateString);
          } 
          catch (ParseException e)
          {
              e.printStackTrace();
          }
          int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
          if(dayOfWeek<0)dayOfWeek=0;
          week=dayNames[dayOfWeek];
          return week;
    }
    /**
     * <p>以基准日期的天数获得时间对应的星期数,2016 1 29-->星期五</p>
     * @param dateString
     */
    public static String getWeek2(int year,int month,int day)
    {
    	String week="";
        int count=0,i=1,j=1970; 
        while(j<year)
        {
            if(j%4==0&&j%100!=0||j%400==0) count+=366;
            else count+=365;
            j++;
        }
        while(i<month)
        {
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) count+=31;
            if(i==4||i==6||i==9||i==11) count+=30;
            if(i==2)
                if(year%4==0&&year%100!=0||year%400==0) count+=29;
                else count+=28;
            i++;
        }
        count+=day;
        switch(count%7)
        {
            case 1:week="星期四";break;
            case 2:week="星期五";break;
            case 3:week="星期六";break;
            case 4:week="星期天";break;
            case 5:week="星期一";break;
            case 6:week="星期二";break;
            case 0:week="星期三";break;
        }
        return week;

    }
    /**
     * <p>根据日期返回对应的星期</p>
     * @param date
     * @return 星期
     */
    public static String getFormatDateWeek(Date date)
    {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE"); 
        return bartDateFormat.format(date);
    }

    /**
     * <p>获得指定格式的时间一周时间,按顺序从周一到周日</p>
     * @param dateFormatStr
     * @return 一周时间(中国人习惯)
     */
    public static String[] getWeekOfDayChina(String dateFormatStr)
    {
           String[] arr=new String[7];
           Date date=null;
           Calendar calendar = Calendar.getInstance();
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
           sdf.applyPattern(dateFormatStr);
           for(int i=1,k=0;i<8;i++,k++)
           {
                calendar.set(Calendar.DAY_OF_WEEK,i);
                if(k>0)
                {
                    date=calendar.getTime();
                    String timeStr = sdf.format(date);
                    arr[i-2]=timeStr;
                       calendar.setTime(date);
                }

           } 
               calendar.add(Calendar.DATE,1);  
            date=calendar.getTime();
            String timeStr = sdf.format(date);
            arr[6]=timeStr;
            return arr;
    }
    
    /**
     * <p>获得指定格式的时间一周时间,按顺序从周日到周六</p>
     * @param dateFormatStr
     * @return 一周时间(西方习惯)
     */
    public static String[] getWeekOfDayEnlish(String dateFormatStr)
    {
           String[] arr=new String[7];
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
           sdf.applyPattern(dateFormatStr);
           int []week={Calendar.SUNDAY,Calendar.MONDAY,Calendar.TUESDAY,Calendar.WEDNESDAY,Calendar.THURSDAY,Calendar.FRIDAY,Calendar.SATURDAY};
           for(int i=0;i<week.length;i++)
           {
                Calendar c = Calendar.getInstance();  
                Date date=new Date();
                c.setTime(date);  
                c.set(Calendar.DAY_OF_WEEK,week[i]);
                String timeStr=new SimpleDateFormat(dateFormatStr).format(c.getTime());
                arr[i]=timeStr;
           } 
           return arr;
    }  
    /**
     * <p>获得时间对应的星期数</p>
     * @param dateString
     */
    public static String  getWeeks(String inputTimeStr)
    {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
          Date date = null;
          try
          {
              date = sdf.parse(inputTimeStr);
          } 
          catch (ParseException e)
          {
              e.printStackTrace();
          }
           SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE"); 
           return bartDateFormat.format(date);
    } 

    /**
     * <p>返回指定格式的第n天后的时间串</p>
     * @param date
     * @param n
     * @param dateFormatStr
     * @return n天后的时间串
     */
    public static String afterNDay(Date date,int n,String dateFormatStr)
    {  
        Calendar c=Calendar.getInstance();  
        DateFormat df=new SimpleDateFormat(dateFormatStr);  
        c.setTime(date);  
        c.add(Calendar.DATE,n);  
        Date d=c.getTime();  
        String s=df.format(d);  
        return s;  
    }  
    /**
     * <p>返回指定格式的第n天后的时间串集合</p>
     * @param date
     * @param n
     * @param dateFormatStr
     * @return n天后的时间串
     */
    public static String[] getAfterNDay(Date date,int n,String dateFormatStr)
    {  
        Calendar c=Calendar.getInstance();
        String []st=new String[n];
        DateFormat df=new SimpleDateFormat(dateFormatStr); 
        for(int i=0;i<n;i++)
        {
            c.setTime(date);  
            c.add(Calendar.DATE,i);  
            Date d=c.getTime();  
            String s=df.format(d); 
            st[i]=s;
        }
 
        return st;  
    }
    /**
     * <p>返回指定格式的第n天前的时间串集合</p>
     * @param date
     * @param n
     * @param dateFormatStr
     * @return n天后的时间串
     */
    public static String[] getBeforNDay(Date date,int n,String dateFormatStr)
    {  
        Calendar c=Calendar.getInstance();
        String []st=new String[n];
        DateFormat df=new SimpleDateFormat(dateFormatStr); 
        for(int i=n;i<0;i++)
        {
            c.setTime(date);  
            c.add(Calendar.DATE,i);  
            Date d=c.getTime();  
            String s=df.format(d); 
            st[i]=s;
        }
 
        return st;  
    }
    
    /**
     * <p>返回指定格式的第n天后的时间串集合</p>
     * @param date
     * @param n
     * @param dateFormatStr
     * @return n天后的时间串
     */
    public static String[] getAfterNDay(int n,String dateFormatStr)
    {  
        Calendar c=Calendar.getInstance();
        String []st=new String[n];
        Date date=new Date();
        
        DateFormat df=new SimpleDateFormat(dateFormatStr); 
        String ss=df.format(date);
        c.setTime(date);  
        c.add(Calendar.DATE,n);  
        Date d=c.getTime();  
        String s=df.format(d); 
        System.out.println(ss);
        System.out.println(s);
        st[0]=ss;
        st[0]=s;
        return st;  
    }

    /**
     * <p>根据指定格式获得时间串</p>
     * @param dateString
     */
    public static String  getFormatDate(String inputTimeStr,String formatStr)
    {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
          Date date = null;
          try
          {
              date = sdf.parse(inputTimeStr);
          } 
          catch (ParseException e)
          {
              e.printStackTrace();
          }
           SimpleDateFormat bartDateFormat = new SimpleDateFormat(formatStr); 
           return bartDateFormat.format(date);
    } 
    /**
     * <p>根据指定格式获得当前时间串</p>
     * @param dateString
     */
    public static String  getFormatCurrentDate(String formatStr)
    {
           Date date = new Date();
           SimpleDateFormat bartDateFormat = new SimpleDateFormat(formatStr); 
           return bartDateFormat.format(date);
    }
    /**  
     * <p>将短时间格式字符串转换为时间formatStr </p>  
     *   
     * @param strDate  
     * @return  
     */  
     public static Date strToDate(String strDate,String formatStr) 
     {   
         SimpleDateFormat formatter = new SimpleDateFormat(formatStr);    
         Date strtodate = null;
        try 
        {
            strtodate = formatter.parse(strDate);
        } 
        catch (ParseException e) 
        {
            e.printStackTrace();
        }   
         return strtodate;   
     }
     /**
      * 获得每个月的天数
      * @param str
      * @return
      */
     public static int getMonthCountDays(String str)
     {
    	 int days=-1;
    	 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

    	 try 
    	 {
	    	 Date date = format.parse(str);
	    	 Calendar calendar = new GregorianCalendar();
	    	 calendar.setTime(date);
	    	 days=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	    	 //System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    	 } 
    	 catch (ParseException e) 
    	 {
    	      e.printStackTrace();
    	 }
    	 return days;
     }
     /**
      * 获得每个月的周数
      * @param date
      * @throws Exception
      */
     public static int getMonthWeeks (String date) 
     {   
    	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");  
    	    Date s=null;
			try {
				s = sdf.parse(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
    	    Calendar ca = Calendar.getInstance();  
    	    ca.setTime(s);  
    	    ca.setFirstDayOfWeek(Calendar.SUNDAY); 
    	    
    	    int week=ca.getActualMaximum(Calendar.WEEK_OF_MONTH);
    	    System.out.println(ca.getActualMaximum(Calendar.WEEK_OF_MONTH));
			return week;  
    }
     /**
      * 
      * @return
      */
     public static int getYear()
     {
    	 Calendar calendar = Calendar.getInstance();
    	 //calendar.add(Calendar.DATE, -1);    //得到前一天
    	 //calendar.add(Calendar.MONTH, -1);    //得到前一个月
    	 int year = calendar.get(Calendar.YEAR);
    	 //int month = calendar.get(Calendar.MONTH)+1; 
    	 return year;
     }
     public static int getMonth()
     {
    	 Calendar calendar = Calendar.getInstance();
    	 //calendar.add(Calendar.DATE, -1);    //得到前一天
    	 //calendar.add(Calendar.MONTH, -1);    //得到前一个月
    	 //int year = calendar.get(Calendar.YEAR);
    	 int month = calendar.get(Calendar.MONTH)+1; 
    	 return month;
     }
     public static int getDay()
     {
    	 Calendar calendar = Calendar.getInstance();
    	 //calendar.add(Calendar.DATE, -1);    //得到前一天
    	 //calendar.add(Calendar.MONTH, -1);    //得到前一个月
    	 //int year = calendar.get(Calendar.YEAR);
    	 int day = calendar.get(Calendar.DATE); 
    	 return day;
     }

     /** 
      * 取得当前日期是多少周 
      * 
      * @param date 
      * @return 
      */ 
      public static int getWeek4Date(Date date) { 
	      Calendar c = new GregorianCalendar(); 
	      c.setFirstDayOfWeek(Calendar.MONDAY); 
	      c.setMinimalDaysInFirstWeek(7); 
	      c.setTime (date);
	      return c.get(Calendar.WEEK_OF_YEAR); 
      }

      /** 
      * 得到某年某周的第一天 
      * 
      * @param year 
      * @param week 
      * @return 
      */ 
      public static Date getFirstDayOfWeek(int year, int week) { 
	      Calendar c = new GregorianCalendar(); 
	      c.set(Calendar.YEAR, year); 
	      c.set (Calendar.MONTH, Calendar.JANUARY); 
	      c.set(Calendar.DATE, 1);
	      Calendar cal = (GregorianCalendar) c.clone(); 
	      cal.add(Calendar.DATE, week * 7);
	      return getFirstDayOfWeek(cal.getTime ()); 
      }
      /** 
       * 取得指定日期所在周的第一天 
       * 
       * @param date 
       * @return 
       */ 
       public static Date getFirstDayOfWeek(Date date) { 
	       Calendar c = new GregorianCalendar(); 
	       c.setFirstDayOfWeek(Calendar.MONDAY); 
	       c.setTime(date); 
	       c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 
	       return c.getTime (); 
       }
       /** 
       * 取得指定日期所在周的最后一天 
       * 
       * @param date 
       * @return 
       */ 
       public static Date getLastDayOfWeek(Date date) { 
	       Calendar c = new GregorianCalendar(); 
	       c.setFirstDayOfWeek(Calendar.MONDAY); 
	       c.setTime(date); 
	       c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 
	       return c.getTime(); 
       } 
       
       /** 
       * 取得当前日期所在周的第一天 
       * 
       * @param date 
       * @return 
       */ 
       public static Date getFirstDayOfWeek() { 
	       Calendar c = new GregorianCalendar(); 
	       c.setFirstDayOfWeek(Calendar.MONDAY); 
	       c.setTime(new Date()); 
	       c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 
	       return c.getTime (); 
       }
       /** 
       * 取得当前日期所在周的最后一天 
       * 
       * @param date 
       * @return 
       */ 
       public static Date getLastDayOfWeek() { 
	       Calendar c = new GregorianCalendar(); 
	       c.setFirstDayOfWeek(Calendar.MONDAY); 
	       c.setTime(new Date()); 
	       c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 
	       return c.getTime(); 
       } 
      /** 
      * 得到某年某周的最后一天 
      * 
      * @param year 
      * @param week 
      * @return 
      */ 
      public static Date getLastDayOfWeek(int year, int week) { 
      Calendar c = new GregorianCalendar(); 
      c.set(Calendar.YEAR, year); 
      c.set(Calendar.MONTH, Calendar.JANUARY); 
      c.set(Calendar.DATE, 1);
      Calendar cal = (GregorianCalendar) c.clone(); 
      cal.add(Calendar.DATE , week * 7);
      return getLastDayOfWeek(cal.getTime()); 
      }

    /**  
     * 根据日期字符串判断当月第几周  
     * @param str  
     * @return  
     * @throws Exception  
     */  
    public static int getWeekNoForMonth(String str) throws Exception{  
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");  
        Date date =sdf.parse(str);  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        //第几周  
        int week = calendar.get(Calendar.WEEK_OF_MONTH);  
        //第几天，从周日开始  
        //int day = calendar.get(Calendar.DAY_OF_WEEK);  
        return week;  
    }  
    /**  
     * 根据日期字符串判断当年第几周  
     * @param str  
     * @return  
     * @throws Exception  
     */  
    public static int getWeekNoForYear(String str) throws Exception{  
    	  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	  Date date = format.parse(str);
    	  Calendar calendar = Calendar.getInstance();
    	  calendar.setFirstDayOfWeek(Calendar.MONDAY);
    	  calendar.setTime(date);
    	  int week=calendar.get(Calendar.WEEK_OF_YEAR);
    	  System.out.println(week); 
          return week;  
    }
    
    /**
    * 字符串转换成日期
    * @param str
    * @return date
    */
    public static Date StrToDate(String str,String formatStr) {
      
       SimpleDateFormat format = new SimpleDateFormat(formatStr);
       Date date = null;
       try {
        date = format.parse(str);
       } catch (ParseException e) {
        e.printStackTrace();
       }
       return date;
    }

}



